package com.ambow.restaurant.sys.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.restaurant.sys.po.User;
import com.ambow.restaurant.sys.service.IUserService;
import com.ambow.restaurant.sys.service.UserServiceIMPL;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/sys/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("name");
		String password = request.getParameter("password");
		
		String method = request.getParameter("method");
		if(method !=null&&method.equals("submitTable")) {
		if(username !=null &&!username.equals("")) {
			IUserService userService = new UserServiceIMPL();
			User user = userService.login(username, password);
			if(user != null) {
				request.getSession().setAttribute("session_user", user);
				response.sendRedirect(getServletContext().getContextPath()+"/sys/IndexServlet");
			}else {
				request.getRequestDispatcher("/jsp/sys/loginsys.jsp").forward(request, response);
			}
			
		}else {
			request.getRequestDispatcher("/jsp/sys/loginsys.jsp").forward(request, response);
		}
			
		}
	}

}
